//
//  SECockpit.cpp
//  Space Explorer
//
//  Created by Yann Blaudin de Thé on 12/04/13.
//  Copyright (c) 2013 TeaTime. All rights reserved.
//

#include "SECrosshair.h"


SECrosshair::SECrosshair(irr::gui::IGUIEnvironment *environment, irr::u32 id) : irr::gui::IGUIElement(irr::gui::EGUIET_ELEMENT, environment, environment->getRootGUIElement(), id, irr::core::rect< irr::s32 >(0,0,0,0))
{
    Texture = 0;
    Color = irr::video::SColor(255,255,255,0);
    Size = 10;
}

SECrosshair::~SECrosshair(void)
{
    if (Texture)
        Texture->drop();
}

void SECrosshair::draw(void)
{
    irr::video::IVideoDriver* driver = Environment->getVideoDriver();
    
    irr::u32 screenW = driver->getScreenSize().Width;
    irr::u32 screenH = driver->getScreenSize().Height;
    irr::u32 cenW = screenW / 2;
    irr::u32 cenH = screenH / 2;
    
    irr::s32 texW, texH;
    
    if (!Texture)
    {        
        // Draw crosshair
        driver->draw2DLine(irr::core::vector2d<irr::s32>(cenW,cenH), irr::core::vector2d<irr::s32>(cenW-Size/2,cenH+Size/2), Color);
        driver->draw2DLine(irr::core::vector2d<irr::s32>(cenW,cenH), irr::core::vector2d<irr::s32>(cenW+Size/2,cenH+Size/2), Color);
        driver->draw2DLine(irr::core::vector2d<irr::s32>(cenW+10,cenH), irr::core::vector2d<irr::s32>(cenW+Size,cenH), Color); //right
        driver->draw2DLine(irr::core::vector2d<irr::s32>(cenW-Size,cenH), irr::core::vector2d<irr::s32>(cenW-10,cenH), Color); //left
    }
    else
    {
        texH = Texture->getOriginalSize().Height;
        texW = Texture->getOriginalSize().Width;
//        driver->makeColorKeyTexture (Texture, irr::core::position2d<irr::s32> (texH/2,texW/2));
        driver->makeColorKeyTexture (Texture, irr::video::SColor (0,0,0,0));
        irr::video::SColor Colors [] = {Color, Color, Color, Color};
        driver->draw2DImage(Texture, irr::core::rect< irr::s32 >(cenW-Size, cenH-Size, cenW+Size, cenH+Size), irr::core::rect< irr::s32 >(0, 0, texW, texH), 0, Colors, true);
    }
    
}

void SECrosshair::setTexture(irr::video::ITexture* texture)
{
    if (Texture)
        Texture->drop();
    
    Texture = texture;
    
    if (Texture)
        Texture->grab();
}

void SECrosshair::setSize(irr::u32 size)
{
    Size = size;
}

void SECrosshair::setColor(irr::video::SColor color)
{
    Color = color;
}
